package com.lmk.pet.service.Impl;

import com.lmk.basic.service.Impl.BaseServiceImpl;
import com.lmk.pet.domain.PetType;
import com.lmk.pet.mapper.PetTypeMapper;
import com.lmk.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper mapper;
    @Override
    public List<PetType> petTypeTree() {
        List<PetType> all = mapper.loadAll();
        Map<Long, PetType> map = new HashMap<>();
        for (PetType petType : all) {
            map.put(petType.getId(),petType);
        }
        List<PetType> petTypeTree = new ArrayList<>();
        for (PetType petType : all) {
            if(petType.getPid()==null){
                petTypeTree.add(petType);
            }else {
                Long parent_id = petType.getPid();

                PetType petTypeParent = map.get (parent_id);

                petTypeParent.getChildren().add(petType);
            }
        }
        return petTypeTree;
    }

}
